<?php
// $Id$

include_once('BaseModel.class.php');

class AddUserModel extends BaseModel
{
    function collect($offset=null,$limit=null,$search=array(),$by='1',$sort='')
	{
		$where = ($search) ? "WHERE LOWER({$search['searchfield']}) LIKE '%{$search['searchtext']}%'" : "WHERE 1";

		if (is_null($offset)) {
	        $query = "SELECT COUNT(*) FROM auths $where AND active=1";
		} else {
	        $query = "SELECT *,CONCAT(`lastname`,', ',`firstname`)as fullname FROM auths $where AND active=1 ORDER BY $by $sort LIMIT $offset,$limit";
		}
		return $this->query($query, true);
    }

    function detail ($id)
    {
        $query = "SELECT * FROM auths WHERE id='$id'";
        return $this->query($query, true);
    }

    /*
     * Add categories and return the new categories_id
     *
     */
    function ins($detail)
    {
        return $this->insert('auths', $detail);
        return $this->last_insert_id('auths');
    }

    /*
     * Update categories for a particular categories_id
     *
     */
    function edit ($id, $detail)
    {
        return $this->update('auths', $detail, "id = '$id'");
    }

    /*
     * Delete categories detail for a particular categories_id
     *
     */
    function deactivate ($id)
    {
        $query = "UPDATE auths SET active = '0' WHERE id='$id'";
        return $this->query($query, false);
    }
    function activate ($id)
    {
        $query = "UPDATE auths SET active = '1' WHERE id='$id'";
        return $this->query($query, false);
    }
	function integrate(){
		$old = $this->get_old();

		$this->select_db('mcs');
		foreach($old as $d){
			//set to blank;
			$d['zzz'] ='';
			$sinfo['stud_no'] = $d['stud_no'];
			$sinfo['fname'] = $d['fname'];
			$sinfo['mname'] = $d['mname'];
			$sinfo['lname'] = $d['lname'];
			$sinfo['gender'] = $d['gender'];
			$sinfo['age'] = $d['age'];
			$sinfo['status'] = $d['status'];
			$sinfo['bdate'] = $d['bdate'];
			$sinfo['pbirth'] = $d['pbirth'];
			$sinfo['add_no'] = $d['zzz'];
			$sinfo['add_street'] = $d['zzz'];
			$sinfo['add_subdbrgy'] = $d['zzz'];
			$sinfo['add_townctry'] = $d['zzz'];
			$sinfo['add_zipcode'] = $d['zzz'];
			$sinfo['home_no'] = $d['homecontact'];
			$sinfo['mobile_no'] = $d['zzz'];
			$sinfo['religion'] = $d['zzz'];
			$sinfo['nationality'] = $d['zzz'];
			$sinfo['mother'] = $d['zzz'];
			$sinfo['mother_add'] = $d['zzz'];
			$sinfo['father'] = $d['zzz'];
			$sinfo['father_add'] = $d['zzz'];
			$sinfo['created_by'] = 'ferdie';
			$sinfo['creation_date'] = $d['creation_date'];
			$sinfo['active'] = $d['active'];

			$this->insert('students', $sinfo);
			$s_id = $this->last_insert_id('students');
                        
                        $d['sy']='3';
                        $einfo['student_id']= $s_id;
                        $einfo['section_offering_id'] = $d['zzz'];
                        $einfo['school_year'] = $d['sy'];
                        $einfo['level_id'] = $d['zzz'];
                        $einfo['en_status'] = $d['zzz'];
                        $einfo['sumr_status'] = $d['zzz'];
                        $einfo['parentline'] = $d['parentline_no'];
                        $einfo['prev_school'] = $d['lastschoolname'];
                        $einfo['prev_schooladd'] = $d['lastschooladd'];
                        $einfo['prev_schooltel'] = $d['lastschoolcontact'];
                        $einfo['prev_yl'] = $d['zzz'];
                        $einfo['prev_grade'] = $d['zzz'];
                        $einfo['guardian'] = $d['guardian_name'];
                        $einfo['guardian_add'] = $d['homeaddress'];
                        $einfo['guardian_tel'] = $d['homecontact'];
                        $einfo['guardian_mobileno'] = $d['parentline_no'];
                        $einfo['guardian_ofcno'] = $d['zzz'];
                        $einfo['fetch_name'] = $d['zzz'];
                        $einfo['fetch_add'] = $d['zzz'];
                        $einfo['fetch_homeno'] = $d['zzz'];
                        $einfo['fetch_mobileno'] = $d['zzz'];
                        $einfo['fetch_serviceplateno'] = $d['zzz'];
                        $einfo['cntrl_no'] = $d['zzz'];
                        $einfo['who_enroll'] = $d['zzz'];
                        $einfo['created_by'] = 'ferdie';
                        $einfo['creation_date'] = $d['creation_date'];
                        $einfo['modified_by'] = $d['zzz'];
                        $einfo['modified_date'] = $d['zzz'];
			$this->insert('enrollments', $einfo);
			$e_id = $this->last_insert_id('enrollments');
		}
	}

	function get_old(){
		$this->select_db('mcsghes');
		return $this->select_all("SELECT * FROM `stud_info`");
	}
}


?>
